from rest_framework import serializers
from .models import myUser, video, comment, like, score, collect


class MyUserSerializer(serializers.ModelSerializer):
    average_score = serializers.SerializerMethodField()  # 改用 SerializerMethodField
    comment_likes = serializers.SerializerMethodField()
    comment_dislikes = serializers.SerializerMethodField()
    comment_count = serializers.SerializerMethodField()
    collect_count = serializers.SerializerMethodField()

    class Meta:
        model = myUser
        fields = ['id', 'username', 'nickName', 'userImg', 'sex', 'average_score', 'comment_likes', 'comment_dislikes',
                  'comment_count','collect_count']

    def get_average_score(self, obj):
        return getattr(obj, 'average_score', 0) or 0

    def get_comment_likes(self, obj):
        return getattr(obj, 'comment_likes', 0) or 0

    def get_comment_dislikes(self, obj):
        return getattr(obj, 'comment_dislikes', 0) or 0

    def get_comment_count(self, obj):
        return getattr(obj, 'comment_count', 0) or 0

    def get_collect_count(self, obj):
        return getattr(obj, 'collect_count', 0) or 0

class VideoSerializer(serializers.ModelSerializer):
    average_score = serializers.SerializerMethodField()  # 改用 SerializerMethodField
    count_score = serializers.SerializerMethodField()
    collect_is = serializers.SerializerMethodField()
    class Meta:
        model = video
        fields = ['id', 'title', 'info', 'img', 'director', 'actor',
                  'time', 'time_tag', 'type_tag', 'length_tag', 'average_score', 'count_score', 'hot_tag', 'class_tag','collect_is']

    def get_average_score(self, obj):
        # 如果 average_score 存在则返回，否则返回 0
        return getattr(obj, 'average_score', 0) or 0

    def get_count_score(self, obj):
        return getattr(obj, 'count_score', 0) or 0

    def get_collect_is(self, obj):
        return getattr(obj, 'collect_is', False) or False


class LikeSerializer(serializers.ModelSerializer):
    user = MyUserSerializer(read_only=True, help_text="点赞用户信息")
    video = VideoSerializer(read_only=True, help_text="点赞视频信息")

    class Meta:
        model = like
        fields = '__all__'


class ScoreSerializer(serializers.ModelSerializer):
    user = MyUserSerializer(read_only=True, help_text="评分用户信息")
    video = VideoSerializer(read_only=True, help_text="评分视频信息")

    class Meta:
        model = score
        fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):
    user = MyUserSerializer(read_only=True, help_text="评论用户信息")
    video = VideoSerializer(read_only=True, help_text="评论视频信息")
    like = LikeSerializer(many=True, help_text="评论的所有点赞信息")  # 添加这一行
    likes_count = serializers.IntegerField(read_only=True, help_text="点赞数")
    dislikes_count = serializers.IntegerField(read_only=True, help_text="点踩数")
    user_score = serializers.IntegerField(read_only=True, help_text="用户评分")

    class Meta:
        model = comment
        fields = ['id', 'user', 'video', 'like', 'content', 'time', 'likes_count', 'dislikes_count', 'user_score']

    def get_likes_count(self, obj):
        return getattr(obj, 'likes_count', 0) or 0

    def get_dislikes_count(self, obj):
        return getattr(obj, 'dislikes_count', 0) or 0

    def get_user_score(self, obj):
        return getattr(obj, 'user_score', 0) or 0


class CollectSerializer(serializers.ModelSerializer):
    user = MyUserSerializer(read_only=True, help_text="收藏用户信息")
    video = VideoSerializer(read_only=True, help_text="收藏视频信息")

    class Meta:
        model = collect
        fields = '__all__'
